﻿<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body,
		html,
		#allmap {
			width: 100%;
			height: 100%;
			overflow: hidden;
			margin: 0;
			font-family: "微软雅黑";
		}

		.inputBox {
			position: absolute;
			top: 10px;
			right: 10px;
			width: 200px;
			height: 20px;
		}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=DPufGhCZqVkZekO0Kiz9Y35sDa9VjZ7x"></script>
	<title>添加行政区划2</title>
</head>

<body>
	<div id="allmap"></div>
	<div class="inputBox">
		<input type="text" value="连云港" id="placeInput">
		<button onclick="getBoundary()">确定</button>
	</div>
</body>

</html>
<script type="text/javascript">
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
	map.enableScrollWheelZoom();

	function getBoundary() {
		var maxLat = 0;
		var minLat = Number.MAX_VALUE;
		var maxLng = 0;
		var minLng = Number.MAX_VALUE;
		var place = document.querySelector("#placeInput").value;
		var bdary = new BMap.Boundary();
		bdary.get(place, function (rs) {       //获取行政区域
			map.clearOverlays();
			var count = rs.boundaries.length; //行政区域的点有多少个
			if (count === 0) {
				alert('未能获取当前输入行政区域');
				return;
			}
			var pointArray = [];
			for (var i = 0; i < count; i++) {
				var ply = new BMap.Polygon(rs.boundaries[i], { strokeWeight: 2, strokeColor: "#ff0000" }); //建立多边形覆盖物
				map.addOverlay(ply);  //添加覆盖物
				pointArray = pointArray.concat(ply.getPath());
			}
			map.setViewport(pointArray);    //调整视野  
			rs.boundaries.forEach(function (out, i) {
				out.split(";").forEach(function (item, j) {
					var point = item.split(",");
					var latPoint = Number(point[1]);
					var lngPoint = Number(point[0]);
					if (latPoint > maxLat) {
						maxLat = latPoint;
					}
					else {
						if (latPoint < minLat)
							minLat = latPoint;
					}
					if (lngPoint > maxLng) {
						maxLng = lngPoint;
					}
					else {
						if (lngPoint < minLng) {
							minLng = lngPoint;
						}
					}
				})
			});
			get("http://localhost:8111/download/"+minLat+"/"+minLng+"/"+maxLat+"/"+maxLng,function (data) {
				debugger;
				
			})
		});
	}


	function addlabel() {
		var pointArray = [
			new BMap.Point(121.716076, 23.703799),
			new BMap.Point(112.121885, 14.570616),
			new BMap.Point(123.776573, 25.695422)];
		var optsArray = [{}, {}, {}];
		var labelArray = [];
		var contentArray = [
			"台湾是中国的！",
			"南海是中国的！",
			"钓鱼岛是中国的！"];
		for (var i = 0; i < pointArray.length; i++) {
			optsArray[i].position = pointArray[i];
			labelArray[i] = new BMap.Label(contentArray[i], optsArray[i]);
			labelArray[i].setStyle({
				color: "red",
				fontSize: "12px",
				height: "20px",
				lineHeight: "20px",
				fontFamily: "微软雅黑"
			});
			map.addOverlay(labelArray[i]);
		}
	}

function get (url, fn) {
    // XMLHttpRequest对象用于在后台与服务器交换数据   
    var xhr = new XMLHttpRequest();            
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function() {
      // readyState == 4说明请求已完成
      if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { 
        // 从服务器获得数据 
        fn.call(this, xhr.responseText);  
      }
    };
    xhr.send();
  }
  //

</script>